package com.woniuxy.project.campus.canteen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project.campus.canteen.mapper.ShopsMapper;
import com.woniuxy.project.campus.canteen.model.BusinessInfo;
import com.woniuxy.project.campus.canteen.mapper.BusinessInfoMapper;
import com.woniuxy.project.campus.canteen.model.Shops;
import com.woniuxy.project.campus.canteen.service.BusinessInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.project.campus.canteenModelform.EditShopTime;
import com.woniuxy.project.campus.canteenModelform.GetShopTimeFoem;
import com.woniuxy.project.campus.util.StaticData;
import com.woniuxy.project.campus.util.TokenTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 猫将军
 * @since 2025年02月17日
 */
@Service
public class BusinessInfoServiceImpl extends ServiceImpl<BusinessInfoMapper, BusinessInfo> implements BusinessInfoService {

    @Resource
    private BusinessInfoMapper businessInfoMapper;
    @Resource
    private ShopsMapper shopsMapper;

    @Override
    public void editShopTime(String token, EditShopTime editShopTime) {
    // TODO: 2025/02/17 修改店铺营业时间
    // 1. 根据token获取用户id
    Integer id = TokenTool.getUserIdByToken(token);
    if (id == null) {
        throw new RuntimeException("无效的token");
    }

    QueryWrapper<Shops> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("account_id", id);
    Shops shops = shopsMapper.selectOne(queryWrapper);
    if (shops == null) {
        throw new RuntimeException("用户未开通商铺");
    }

    BusinessInfo businessInfo = new BusinessInfo();
    businessInfo.setShopId(shops.getId()); // 确保更新的是正确的店铺信息

    try {
        String formattedTimeString = String.format("%s:00", editShopTime.getStartTime());
        // 将格式化后的字符串转换为 Time 对象
        Time time = Time.valueOf(formattedTimeString);
        businessInfo.setOpeningHours(time);

        String time2 = String.format("%s:00", editShopTime.getEndTime());
        Time time1 = Time.valueOf(time2);
        businessInfo.setClosingHours(time1);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("时间格式不正确", e);
    }

    String[] businessDays = editShopTime.getBusinessDays();
    if (businessDays != null && businessDays.length > 0) {
        businessInfo.setMonday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setTuesday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setWednesday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setThursday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setFriday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setSaturday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setSunday(StaticData.BUSINESS_DAY_CLOSE);

        for (String day : businessDays) {
            switch (day) {
                case "星期一":
                    businessInfo.setMonday(StaticData.BUSINESS_DAY_OPEN);
                    break;
                case "星期二":
                    businessInfo.setTuesday(StaticData.BUSINESS_DAY_OPEN);
                    break;
                case "星期三":
                    businessInfo.setWednesday(StaticData.BUSINESS_DAY_OPEN);
                    break;
                case "星期四":
                    businessInfo.setThursday(StaticData.BUSINESS_DAY_OPEN);
                    break;
                case "星期五":
                    businessInfo.setFriday(StaticData.BUSINESS_DAY_OPEN);
                    break;
                case "星期六":
                    businessInfo.setSaturday(StaticData.BUSINESS_DAY_OPEN);
                    break;
                case "星期日":
                    businessInfo.setSunday(StaticData.BUSINESS_DAY_OPEN);
                    break;
                default:
                    // 如果有无效的工作日，可以选择忽略或抛出异常
                    throw new RuntimeException("无效的工作日: " + day);
            }
        }
     } else {
        businessInfo.setMonday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setTuesday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setWednesday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setThursday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setFriday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setSaturday(StaticData.BUSINESS_DAY_CLOSE);
        businessInfo.setSunday(StaticData.BUSINESS_DAY_CLOSE);
     }

     businessInfoMapper.insert(businessInfo);
}


    @Override
    public GetShopTimeFoem getShopTime(String token) {
        // TODO: 2025/02/17 获取店铺营业时间
        // 1. 根据token获取用户id
        Integer id = TokenTool.getUserIdByToken(token);
        QueryWrapper<Shops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", id);
        Shops shops = shopsMapper.selectOne(queryWrapper);
        if (shops == null) {
            throw new RuntimeException("用户未开通商铺");
        }
        QueryWrapper<BusinessInfo> businessInfoQueryWrapper = new QueryWrapper<>();
        businessInfoQueryWrapper.eq("shop_id", shops.getId());
        BusinessInfo businessInfo = businessInfoMapper.selectOne(businessInfoQueryWrapper);
        GetShopTimeFoem getShopTimeFoem = new GetShopTimeFoem();
        getShopTimeFoem.setStart(businessInfo.getOpeningHours().toString().substring(0, 5));
        getShopTimeFoem.setEnd(businessInfo.getClosingHours().toString().substring(0, 5));

        return getShopTimeFoem;
    }

}
